;(function(){
      //本地存储
	var _storage = {
		"set":function(key,value){
		  window.localStorage.removeItem(key);
		  window.localStorage.setItem(key,value);
		},
		"get":function(key){
		  return window.localStorage.getItem(key);
		},
		"remove":function(key){
		  window.localStorage.removeItem(key);
		},
		"all":function(key){
		  return window.localStorage;
		},
		"clear":function(key){
		  window.clear();
		}
	}

	var _login = function(channelId,type,callback){
        var data = {};
        type==1 ? data['mixnick']=channelId : data['openid']=channelId;
        $.ajax({
          type:'POST',
          url:'/lux/login',
          dataType:'json',
          data:data,
          success:function(result){
            console.log(result);
            callback(result);
          },
          error:function(error){
            console.log(error);
            alert('网络异常，自动刷新中...');
            //location.href = 'https://arbuy.ews.m.jaeapp.com/aogamefruit/startar.html';
          }
        });
	}

/*	var _startgame = function(callback){
		var token = _storage.get('token');
        $.ajax({
          type:'POST',
          url:'/lux/chance/use',
          dataType:'json',
          data:{"token":token},
          success:function(result){
            console.log(result);
            callback(result);
          },
          error:function(error){
            console.log(error);
            alert('网络异常，自动刷新中...');
            //location.href = 'https://arbuy.ews.m.jaeapp.com/aogamefruit/startar.html';
          }
        });
	}*/

	var _lottery = function(callback){
    var token = _storage.get('token');
        $.ajax({
          type:'POST',
          url:'/lux/award/get',
          dataType:'json',
          data:{"token":token},
          success:function(result){
            console.log(result);
            callback(result);
          },
          error:function(error){
            console.log(error);
            alert('网络异常，自动刷新中...');
            //location.href = 'https://arbuy.ews.m.jaeapp.com/aogamefruit/startar.html';
          }
        });
	}

/*	var _share = function(callback){
		var token = _storage.get('token');
        $.ajax({
          type:'POST',
          url:'/lux/chance/get',
          dataType:'json',
          data:{"token":token,"method":"share"},
          success:function(result){
            console.log(result);
            callback(result);
          },
          error:function(error){
            console.log(error);
            alert('网络异常，自动刷新中...');
            //location.href = 'https://arbuy.ews.m.jaeapp.com/aogamefruit/startar.html';
          }
        });
	}*/

	var _submitInfo = function(options,callback){
		var token = _storage.get('token');
        $.ajax({
          type:'POST',
          url:'/lux/info/submit',
          dataType:'json',
          data:{
          	"token":token,
	          "realname":options.realname,
	          "phone":options.phone,
	          "address":options.address
	        },
          success:function(result){
            console.log(result);
            callback(result);
          },
          error:function(error){
            console.log(error);
            alert('网络异常，请重试...');
            // //location.href = 'https://arbuy.ews.m.jaeapp.com/aogamefruit/startar.html';
          }
        });
	}

	var _interface = {};

	_interface.login = function(callback){

		if(Tida.isSupportedApp){
          var options = {
            sellerNick: ""
          };
          Tida.mixNick(options, function (data) {
            if (typeof(data) == "undefined" || typeof(data.mixnick) == "undefined") {
              alert("请登录后重试");//安卓版天猫客户端未登录时
              return;
            }
            var mixnick = encodeURIComponent(data.mixnick);
            _login(mixnick,1,function(result){
              if(!result.errCode){
                _storage.set('token',result.data.token);
                callback(result.data.chance,result.data.avatar,decodeURIComponent(mixnick).substring(0,4)+'***');
              }else{
              	//location.href = 'https://arbuy.ews.m.jaeapp.com/aogamefruit/startar.html';
              }
            });
          });
      }else if(0){
        $(document).ready(function(){
          $('body').html('请用手淘打开');
        });
      }else{
        var openid = $('input[name=m0j1t2]').val()||'';
        var avatar = $('input[name=m0j1t2Avatar]').val()||'';
        var nickname = $('input[name=m0j1t2Nickname]').val()||'';
        // alert(avatar);
        _login(openid,2,function(result){
           if(!result.errCode){
            	_storage.set('token',result.data.token);
            	callback(result.data.chance,avatar,nickname);
           }else{
          		//location.href = 'https://arbuy.ews.m.jaeapp.com/aogamefruit/startar.html';
           }
        });
      }
	}

/*	_interface.startgame = function(callback){
		_startgame(function(result){
			if(!result.errCode){
				callback(true,result.data.chance,result.data.share);				
			}else if(result.errCode==4004){
				callback(false,result.data.chance,result.data.share);	
			}else{
				alert('网络异常，自动刷新中...');
				//location.href = 'https://arbuy.ews.m.jaeapp.com/aogamefruit/startar.html';
			}
		});
	}*/

/*	_interface.share = function(callback){
		if(Tida.isSupportedApp){
			// var url = 'https://vrbuy.ews.m.jaeapp.com/fairwhale/index.html?mytoken='+window.usercheckData.newurl;
		    Tida.share({
		        title: 'A.O.SMITH智净卫士争夺战',
		        text: 'A.O.SMITH智净卫士争夺战',
		        content: 'A.O.SMITH智净卫士争夺战',
		        url : 'https://arbuy.ews.m.jaeapp.com/aogamefruit/startar.html',
		        image: 'https://arbuy.ews.m.jaeapp.com/aogamefruit/static/image/slogo.jpg',
		        'targets': [0,1,2,3,4,5,6,7],
		        'weixinAppId': "",
		        'weixinMsgType': 'image',
		        'isAliUrl': true,
		    }, function(data) {
		        //领取优惠券
		        console.log('share')
		    });			
		}
    //_czc.push(["_trackEvent",'aogamefruit','分享','','','']);
		_share(function(result){
			if(!result.errCode){
				callback(true,result.data.chance,result.data.share);				
			}else if(result.errCode==4004){
				callback(false,result.data.chance,result.data.share);	
			}
		});
	}*/

	_interface.lottery = function(callback){
		_lottery(function(result){
			if(!result.errCode){
        //_czc.push(["_trackEvent",'aogamefruit','抽奖_'+result.data.newAward,'','','']);
				callback(result.data.newAward);				
			}else{
				alert('网络异常，自动刷新中...');
				// //location.href = 'https://arbuy.ews.m.jaeapp.com/aogamefruit/startar.html';
			}
		});
	}

	_interface.submitInfo = function(options,callback){
		_submitInfo(options,function(result){
			if(!result.errCode){
				callback(true);				
			}else{
				callback(false);		
			}
		});
	}

	window.interface = _interface;

})()